package ee.videorentalstore.protocols.commands.filmmanagercommands;

import java.math.BigDecimal;
import java.util.List;
import ee.videorentalstore.filmsrentedbyusers.FilmRentedByUser;
import ee.videorentalstore.filmsrentedbyusers.FilmsRentedByUsersManager;
import ee.videorentalstore.protocols.commands.Command;
import ee.videorentalstore.users.User;
import ee.videorentalstore.users.UsersDatabase;
import ee.videorentalstore.utils.PriceCalculator;

public class GetActiveRentalsForAllCustomersCommand extends Command
{
	@Override
	public String getCommandDescription() 
	{
		return "GET ACTIVE RENTALS FOR ALL CUSTOMERS";
	}
	
	
	@Override
	public void executeCommand(String inputStreamInstructionsToProcess) 
	{
		for (User user : UsersDatabase.getInstance().getAllUsers())
		{
			constructActiveRentalsSummaryForSingleCustomer(user);
		}
	}
	
	
	private void constructActiveRentalsSummaryForSingleCustomer(User user)
	{
		BigDecimal totalPrice = new BigDecimal("0");
		
		List<FilmRentedByUser> listOfAllFilmsRentedByUser = 
				FilmsRentedByUsersManager.getInstance().getListOfAllFilmsRentedByUser(user);
		if (!listOfAllFilmsRentedByUser.isEmpty())
		{
			System.out.println("User with id " + user.getUserID() + " and named " +
					user.getUserName() + " active rentals summary: \n");
			
			for (FilmRentedByUser filmRentedByUser : listOfAllFilmsRentedByUser)
			{
				boolean filmRentalIsPaidWithBonusPoints = filmRentedByUser.getIsPaidWithBonusPoints();
				if (filmRentalIsPaidWithBonusPoints)
				{
					displaySingleRentalSummaryWhenRentalIsPaidWithBonusPoints(filmRentedByUser);
				}
				else
				{
					displayDefaultSingleRentalSummary(filmRentedByUser, totalPrice);
					totalPrice = totalPrice.add(PriceCalculator.calculateRentalFee(
							filmRentedByUser.getNumberOfRentalDays(), 
									filmRentedByUser.getFilm().getType()));
				}
			}
			
			System.out.println("Total price: " + totalPrice.toString());
			System.out.println();
			System.out.println("------------------------------------------------------------------------------------");
		}
	}
	
	
	private void displaySingleRentalSummaryWhenRentalIsPaidWithBonusPoints(
			FilmRentedByUser filmRentedByUser)
	{
		System.out.println(filmRentedByUser.getFilm().getName() + 
				"(" + filmRentedByUser.getFilm().getType().toString() + ") " + 
				filmRentedByUser.getNumberOfRentalDays() + " days " +
				"(Paid with bonus points)");
	}
	
	
	private void displayDefaultSingleRentalSummary(
			FilmRentedByUser filmRentedByUser, BigDecimal totalPrice)
	{
		System.out.println(filmRentedByUser.getFilm().getName() + 
				"(" + filmRentedByUser.getFilm().getType().toString() + ") " + 
				filmRentedByUser.getNumberOfRentalDays() + " days " +
				PriceCalculator.calculateRentalFee(filmRentedByUser.getNumberOfRentalDays(), 
						filmRentedByUser.getFilm().getType()) + " EUR");
	}
}
